<?php

namespace app\webapi\vms\service;

use app\webapi\common\BaseService;
use app\webapi\vms\model\SlideModel;
use library\OWException;

/**
 * @幻灯片服务类
 */
class SlideService extends BaseService
{
    /**
     * @desc 获取幻灯片列表
     * @param $website_id
     * @return SlideModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/12-16:59
     */
    public function getList($page_no, $page_size, $website_id)
    {
        $fields=['id', 'cover', 'sort','url_path','create_time'];
        $where = ['website_id' => $website_id, 'delete_time' => 0];
        $list = SlideModel::field($fields)
            ->where($where)
            ->order('create_time','desc')
            ->paginate(['page' => $page_no, 'list_rows' => $page_size]);;
        return $list;
    }

    /**
     * @desc 获取幻灯片详情
     * @param $website_id
     * @param $id
     * @return SlideModel|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/12-17:01
     */
    public function getInfo($id, $website_id)
    {
        $where = ['website_id' => $website_id, 'id' => $id];
        $fields=['id', 'cover', 'sort','url_path','create_time'];
        $info = SlideModel::field($fields)->where($where)->find()??[];
        return $info;
    }

    /**
     * @desc 编辑幻灯片
     * @param $params
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/12-17:11
     */
    public function edit($params, $website_id)
    {
        $where = ['website_id' => $website_id];
        if (isset($params['id']) && !empty($params['id']) && $params['id'] != '') {
            $where['id'] = $params['id'];
            $where['delete_time'] = 0;
            $info = SlideModel::field(['id'])->where($where)->find();
            if (empty($info)) {
                throw (new OWException('幻灯片信息不存在', $params['id']));
            }
            //更新
            unset($params['id']);
            (new SlideModel())->where($where)->save($params);
        } else {
            //写入
            (new SlideModel())->save($params);
        }
        return true;
    }

    /**
     * @desc 删除幻灯片
     * @param $ids
     * @param $website_id
     * @return mixed
     * @author zhaozhiwei
     * @time 2023/12/12-17:30
     */
    public function delete($ids, $website_id)
    {
        $where=['id' => $ids, 'website_id' => $website_id];
        SlideModel::where($where)->update(['delete_time' => time()]);
        return $ids;
    }

    /**
     * @desc 幻灯片排序
     * @param $website_id
     * @return true
     * @author zhaozhiwei
     * @time 2023/12/12-17:31
     */
    public function sort($json_array, $website_id)
    {
        $where=['website_id' => $website_id];
        foreach ($json_array as $v) {
            $where['id']=$v['id'];
            SlideModel::where($where)->save(['sort' => $v['sort']]);
        }
        return true;
    }
}